package com.itclass.auth.listener;




import com.itclass.auth.handler.authentication.AbstractAuthenticationFailureHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AuthenticationFailureApplicationListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
    @Autowired
    private List<AbstractAuthenticationFailureHandler> authenticationFailureAdapters;

    @Override
    public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
        Authentication authentication = (Authentication) event.getSource();
        for (AbstractAuthenticationFailureHandler adapter : authenticationFailureAdapters) {
            if (adapter.supports(authentication.getClass())) {
                adapter.handle(authentication);
                break;
            }
        }

    }
}
